<?php 
if(!isset($hide))
	$hide = array();
$owner_param = '';
if(!empty($owner_id)) $owner_param = '&owner_id=' . $owner_id;
	
?>
<div class="showTable">
  <?php
	if($objDef->showExtraTemplate!=''){
		include_partial($objDef->showExtraTemplate, array('self'=>$self, 'objDef' => $objDef)); 
	}?>
	  <?php echo $objDef->renderDefault($self,$hide,'div',true); ?>
</div>      
<hr />
<!-- Items del objeto -->
<?php foreach($objDef->attributes as $fieldName => $att):?>
	  <?php if($att['type']=='List' and $att['show']):?>
      <?php 
      			/* Es la relacion principal, va del id de este objeto al owner del item...
      			 * El nombre del atributo tiene que ser igual al foreignalias del atributo que vincula al item
      			 * */
      			$relations = $self->getTable()->getRelations();
 	  			if(isset($relatedObjDef))
    				unset($relatedObjDef);
				foreach ($relations as $name => $relation) {
					if($relation->getType() == 1){
		    			if($fieldName==$name){
    						$relatedClass = $relation->getClass();
    						$relatedObjDef = new objectDef($relatedClass);
    						$addNew = !$att['readOnly'];
    						$q = $relatedObjDef->getBaseQuery();
    						$ownerAtt = $relation->getForeign();
    						$q->addWhere('a.' . $ownerAtt . ' = ?', $self->id);
    						$q->setHydrationMode(Doctrine::HYDRATE_ARRAY);    						
    						echo $relatedObjDef->renderListTable($q->execute(), $module, null, true, $addNew, $addActions = true, $addExports = false, $tableClass = 'list_table', $addOrderLink = false, $orderFunctionParams = '', $ajaxActions = false, $ownerFieldName ='', $exportParams = '', $self->id);
    						//$fieldName = $name;
		    			}
					}
				}
      		?>
  <?php endif;?>
<?php endforeach;?>
<?php 
	$q = Doctrine::getTable('attachment')
		->createQuery('m')
		->where('owner_id= ?',$self->id)
		->andWhere("ownerclass = ?",get_class($self));
	$q->setHydrationMode(Doctrine_Core::HYDRATE_ARRAY);	
	$records =	$q->execute();
	if(count($records) > 0)
	{
		$objectDef = new objectDef('attachment');
		echo $objectDef->renderListTable($records,$module,array('public'),true,false,false);
	}
?>
<div class="buttons">
	<div class="innerButtons">
      <?php if(!$objDef->isReadOnly):?>
		<?php echo link_to('<img title="'.__('Edit').'" src="/images/icons/64/edit.png"/>', $module.'/edit?id='.$self->id.'&className='.$className . $owner_param);?>
	  <?php endif;?>
      <?php if($objDef->canBeDeleted):?>
          <?php echo link_to('<img title="'.__('Delete').'" src="/images/icons/64/delete.png"/>', $module.'/delete?id='.$self->id.'&className='.$className .$owner_param,  array('method' => 'delete', 'confirm' => __('Are you sure'))) ?>
      <?php endif;?>
      <?php if($owner_param=='' or $owner_id == $self->getId()){	  
	  			echo link_to( '<img title="'.__('Go Back to list').'" src="/images/icons/64/confirm.png"></img>', $module.'/index?className='.$className);
		    }else{
		    	if(!isset($ownerModule) or $ownerModule=='')
		    		$ownerModule = $module;
		    	
		    	echo link_to( '<img title="' . __('Go Back').'" src="/images/icons/64/confirm.png"></img>', $ownerModule.'/show?id='.$owner_id.'&className='.$ownerClass);
		    }?>
	</div>  
</div>

